//
//  BMListTableViewCell.m
//  bum
//
//  Created by xingshuai on 2020/12/29.
//

#import "BMListTableViewCell.h"

@interface BMListTableViewCell ()

@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) UILabel *label;

@end

@implementation BMListTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.img = [[UIImageView alloc]init];
        self.img.backgroundColor = UIColor.orangeColor;
        self.img.layer.cornerRadius = 5;
        self.img.layer.masksToBounds = YES;
        self.img.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:self.img];
        [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(15);
            make.size.mas_offset(CGSizeMake(80, 80));
        }];
        
        self.label = [[UILabel alloc]init];
        self.label.text = @"album";
        self.label.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.label];
        [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.img);
            make.left.equalTo(self.img.mas_right).offset(10);
            make.right.equalTo(self.contentView).offset(-15);
            make.height.mas_offset(50);
        }];
    }
    return self;
}

-(void)setAlbumModel:(BMAlbumModel *)albumModel {
    _albumModel = albumModel;
    self.label.text = [NSString stringWithFormat:@"%@ (%ld)",albumModel.collectionTitle,[albumModel.collectionNumber integerValue]];
    
    CGFloat imageWidth = self.contentView.frame.size.width*[UIScreen mainScreen].scale;
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.synchronous = true;
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    options.networkAccessAllowed = YES;
    
    typeof(self) __weak weakSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[PHImageManager defaultManager]requestImageForAsset:albumModel.firstAsset targetSize:CGSizeMake(imageWidth, imageWidth) contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            dispatch_async(dispatch_get_main_queue(), ^{
                typeof(weakSelf) __strong strongSelf = weakSelf;
                strongSelf.img.image = result;
            });
        }];
    });
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
